package service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import security.MySecurityContextHandle;
import service.interf.IAgentBaseDef;
import service.interf.IIfwAgentRegInfo;
import service.interf.security.IAuthority;

import common.impl.MD5Transform;
import common.impl.exception.ServiceException;

import dao.interf.IAgentBaseConfigDAO;
import dao.interf.IAgentBaseDefDAO;
import dao.interf.IIfwAgentRegInfoDAO;
import dao.interf.IUserDAO;
import dao.pojo.AgentBaseConfig;
import dao.pojo.AgentBaseDef;
import dao.pojo.IfwAgentRegInfo;
import dao.pojo.User;

public class IfwAgentRegInfoImpl extends BaseDataServiceImpl implements
		IIfwAgentRegInfo {
	private IIfwAgentRegInfoDAO iIfwAgentRegInfoDAO;
	private IAuthority authorityImpl;
	private IUserDAO iUserDAO;
	private User user;
	private IAgentBaseDefDAO agentBaseDefDao;
	private IAgentBaseConfigDAO agentBaseConfigDao;

	public IAuthority getAuthorityImpl() {
		return authorityImpl;
	}

	public void setAuthorityImpl(IAuthority authorityImpl) {
		this.authorityImpl = authorityImpl;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public IUserDAO getiUserDAO() {
		return iUserDAO;
	}

	public void setiUserDAO(IUserDAO iUserDAO) {
		this.iUserDAO = iUserDAO;
	}

	public IIfwAgentRegInfoDAO getiIfwAgentRegInfoDAO() {
		return iIfwAgentRegInfoDAO;
	}

	public void setiIfwAgentRegInfoDAO(IIfwAgentRegInfoDAO iIfwAgentRegInfoDAO) {
		this.iIfwAgentRegInfoDAO = iIfwAgentRegInfoDAO;
	}
	
	public void setAgentBaseDefDao(IAgentBaseDefDAO agentBaseDefDao) {
		this.agentBaseDefDao = agentBaseDefDao;
	}
	
	public void setAgentBaseConfigDao(IAgentBaseConfigDAO agentBaseConfigDao) {
		this.agentBaseConfigDao = agentBaseConfigDao;
	}

	public String addAgent(IfwAgentRegInfo model, String password)
			throws Exception {
		// 存user
		String userName = model.getAgentName();
		long user_total = iUserDAO.getPropertiesCount(
				new String[] { "userName" }, new Object[] { userName }, null,
				null);
		if (user_total > 0)
			throw new ServiceException("用户名已存在!");
		User user = new User();
		user.setUserName(userName);
		user.setPassword((MD5Transform.EncoderByMd5(password, "utf-8")));
		user.setEnabled((short) 3);
		iUserDAO.save(user);
		authorityImpl.addUserToGroup(user.getUserId(), "agent");

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String date = df.format(new Date());
		model.setAgentRegTime(date);
		model.setAgentPoint(0);
		iIfwAgentRegInfoDAO.save(model);
		// 为代理商加入默认基数
		List<AgentBaseDef> agentBaseDefL = agentBaseDefDao.findAll(null);
		Integer agentBase = 600;
		if(null != agentBaseDefL && !agentBaseDefL.isEmpty())
			agentBase = agentBaseDefL.get(0).getAgentBase();
		if(null == agentBase)
			agentBase = 600;
		AgentBaseConfig agentBaseConfig = new AgentBaseConfig();
		agentBaseConfig.setAgentName(userName);
		agentBaseConfig.setAgentBase(agentBase);
		agentBaseConfig.setNote("无");
		agentBaseConfigDao.save(agentBaseConfig);
		return "1";

	}

	public String consumpPoint(Integer agentIndex, Integer point)
			throws Exception {
		IfwAgentRegInfo agentRegInfo = iIfwAgentRegInfoDAO.findById(agentIndex);
		if (agentRegInfo == null) {
			throw new ServiceException("系统异常,请稍候再试!");
		}

		if (agentRegInfo.getAgentPoint() == 0) {
			return "2";
		} else if (agentRegInfo.getAgentPoint() < point) {
			return "2";
		} else {
			Integer agentPoint = agentRegInfo.getAgentPoint();
			agentPoint = agentPoint - point;
			agentRegInfo.setAgentPoint(agentPoint);
		}
		iIfwAgentRegInfoDAO.update(agentRegInfo);
		return "1";
	}

	public String changeAgent(IfwAgentRegInfo model) throws Exception {
		String agentName = model.getAgentName();
		List<IfwAgentRegInfo> findByProperties = iIfwAgentRegInfoDAO
				.findByProperties(new String[] { "agentName" },
						new Object[] { agentName });
		if (findByProperties != null && findByProperties.size() != 0) {
			if (!agentName.equals(MySecurityContextHandle.getUserName())) {
				throw new ServiceException("用户名已存在!");
			}
		}
		Integer agentIndex = MySecurityContextHandle.getAgentIndex();
		IfwAgentRegInfo agentRegInfo = iIfwAgentRegInfoDAO.findById(agentIndex);
		// 更新user
		List<User> findByProperties1 = iUserDAO.findByProperties(
				new String[] { "userName" }, new Object[] { agentRegInfo
						.getAgentName() });
		User user = new User();
		user = findByProperties1.get(0);
		user.setUserName(agentName);
		iUserDAO.update(user);
		// 更新agent
		agentRegInfo.setAgentName(agentName);
		agentRegInfo.setAgentAddr(model.getAgentAddr());
		agentRegInfo.setAgentPhone1(model.getAgentPhone1());
		agentRegInfo.setAgentPhone2(model.getAgentPhone2());
		iIfwAgentRegInfoDAO.update(agentRegInfo);

		return "1";
	}

	public String changePwd(IfwAgentRegInfo model, String oldPwd,
			String password) throws Exception {
		Integer agentIndex = MySecurityContextHandle.getAgentIndex();
		IfwAgentRegInfo agentRegInfo = iIfwAgentRegInfoDAO.findById(agentIndex);
		agentRegInfo.setAgentEmail(model.getAgentEmail());
		iIfwAgentRegInfoDAO.update(agentRegInfo);
		List<User> userL = iUserDAO.findByProperties(new String[] { "userName",
				"password" }, new Object[] { agentRegInfo.getAgentName(),
				MD5Transform.EncoderByMd5(oldPwd, "utf-8") });
		if (null == userL || userL.isEmpty()) {
			throw new ServiceException("原密码错误");
		}
		User user = userL.get(0);
		user.setPassword((MD5Transform.EncoderByMd5(password, "utf-8")));
		iUserDAO.update(user);
		return "1";
	}

}
